package com.wing.product.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.ProductItemCategory;
import com.wing.product.model.request.ProductItemCategoryForm;
import com.wing.product.model.response.ProductItemCategoryVO;
import com.wing.product.service.ProductItemCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xiayang
 */
@Validated
@Api(value = "Admin-产品检测项分类管理", tags = "#/product/productItemCategory")
@RestController("productItemCategoryController")
@RequestMapping("product/admin/productItemCategory")
@RequiredArgsConstructor
public class ProductItemCategoryController {

    private final ProductItemCategoryService productItemCategoryService;


    @ApiOperation(value = "查询所有产品检测项分类", notes = "搜索")
    @GetMapping("/list")
    public PageResult<ProductItemCategoryVO> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(defaultValue = "asc") String order,
            String name
    ) {
        Page<ProductItemCategory> pg = new Page<>(page, limit);
        Map paramMap = new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order", StrUtil.toUnderlineCase(order));
        paramMap.put("name", name);
        IPage<ProductItemCategoryVO> list = productItemCategoryService.pageList4app(pg, paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加产品检测项分类", notes = "添加")
    @PostMapping("/add")
    public JsonResult add(ProductItemCategoryForm productItemCategoryForm) {
        if (productItemCategoryService.add(productItemCategoryForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    @ApiOperation(value = "修改产品检测项分类", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(ProductItemCategoryForm productItemCategoryForm) {
        ProductItemCategory productItemCategory = productItemCategoryService.getById(productItemCategoryForm.getId());
        if (productItemCategory == null) {
            return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create().setEditable(ProductItemCategory.class).setIgnoreError(true).setIgnoreNullValue(true);
        BeanUtil.copyProperties(productItemCategoryForm, productItemCategory, copyOptions);
        if (productItemCategoryService.update(productItemCategoryForm) >= 0) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }


    @ApiOperation(value = "删除产品检测项分类", notes = "删除")
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long productItemCategoryId) {
        if (productItemCategoryService.del(productItemCategoryId) >= 0) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }

    @ApiOperation(value = "启用产品检测项分类", notes = "启用")
    @PostMapping("/enable")
    public JsonResult enable(ProductItemCategoryForm productItemCategoryForm) {
        if (productItemCategoryService.enable(productItemCategoryForm.getId()) >= 0) {
            return JsonResult.success("启用成功");
        }
        return JsonResult.fail("启用失败");
    }

    @ApiOperation(value = "停用检测项分类", notes = "停用")
    @PostMapping("/disable")
    public JsonResult disable(ProductItemCategoryForm productItemCategoryForm) {
        if (productItemCategoryService.disable(productItemCategoryForm.getId()) >= 0) {
            return JsonResult.success("停用成功");
        }
        return JsonResult.fail("停用失败");
    }

}
